package com.projectName.www.servlet;


import com.projectName.www.service.UserServiceImpl;
import com.projectName.www.po.Dish;
import com.projectName.www.po.User;
import com.projectName.www.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/searchServlet")
public class SearchServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        UserService service=new UserServiceImpl();
        String keyWord = request.getParameter("keyWord");
        System.out.println("keyWord="+keyWord);
        if("1".equals(keyWord)){
            //搜索菜品
            List<Dish>dishList=service.findDishByName(request.getParameter("searchText"));
            request.setAttribute("dishs",dishList);
            request.getRequestDispatcher("/dishlist.jsp").forward(request,response);
        }else{
            List<User> users=service.findUserByName(request.getParameter("searchText"));
            request.setAttribute("users",users);
            request.getRequestDispatcher("/userlist.jsp").forward(request,response);

        }
    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
